//设计获奖地域等级下拉选框

//忽略VSCode命名报警问题
// ignore_for_file: camel_case_types, non_constant_identifier_names, unused_field, prefer_typing_uninitialized_variables, must_be_immutable

import 'package:flutter/material.dart';

class DropDownMenu_Region_level extends StatefulWidget {

  String new_region_level;   // 从程序外部传入全局性new_region_level变量
  final Function(String) onChanged;   // 设置回调函数用于选项变化时更新传入的全局变量
  final Function(String) onInit;   // 设置回调函数启动时给new_region_level赋全局变量

  DropDownMenu_Region_level({
    super.key,
    required this.new_region_level,
    required this.onChanged,
    required this.onInit,
  });
  @override
  State<DropDownMenu_Region_level> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<DropDownMenu_Region_level> {
  
  final List<String> data = ["国家级","省级","市/县及以下"];
  late String _selectedRegionLevel;   // 专门用于存储下面被选中的变量,之所以要这么设置是防止对话框一结束就导致变量重置,所以搞一个页面生命周期内的变量防止这样的情况发生              
  // 覆写类的构造函数
  @override
  void initState ()
    {
      super.initState();
      setState(() {      // StatefulWidget里要赋初值的话必须用setState更新状态!
        _selectedRegionLevel=data[0];
      });
      widget.onInit(data.first);
    }

  @override
  Widget build(BuildContext context) {
    // widget.new_region_level=data.first;    // 提前给new_region_level赋给初值放后面显示时报错
    widget.new_region_level=data.first;   // 先给new_region_level赋个初值考虑用户未进行选择的情况
    return Row(   // 设置列式布局方便居中对齐
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        DropdownButton<String>(
          menuWidth: 120,
          alignment: AlignmentDirectional.bottomCenter,   // 设置选中的选项文字显示在中间
          value: _selectedRegionLevel,
          items: [
            DropdownMenuItem(
              value: "国家级",
              child: Center(child: Text("国家级")),
            ),
            DropdownMenuItem(
              value: "省级",
              child: Center(child: Text("省级")),
            ),
            DropdownMenuItem(
              value: "市/县及以下",
              child: Center(child: Text("市/县及以下")),
            ),
          ], 
          onChanged: (newValue){
            if (newValue!.isEmpty)    // 如果用户没有做选择直接选择默认选项
              {
                newValue=data.first;
              }
            setState(() {
              _selectedRegionLevel=newValue!;
            });
            widget.onChanged(newValue);   // 更新外部变量
          },
        ),
      ],
    );
  }
  /*
  return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        DropdownMenu<String>(
          menuHeight: 200,
          initialSelection: data.first,
          onSelected: _onselect,
          dropdownMenuEntries:_buildMenuList(data),
          controller: widget.textcontroller_region_level,
        ),
      ],
    );
  */
}
